<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');


class PhotoField {

	function uploadPhoto(&$bean, $event, $arguments)
	{

		$GLOBALS['log']->debug("Photo Field->uploadPhoto");

		//this is the name of the field that is created in studio for the photo
		$GLOBALS['log']->debug("Photo Field: Uploaded Files Array: ".print_r($_FILES,true));

		if(!empty($_FILES)) {
			require_once('include/utils.php');
			global $sugar_config;
			
			foreach($_FILES as $field_name=>$image_array) {
				
				if($bean->field_defs[$field_name]['type'] != 'photo') continue; //this is not a photo field...ignore
				
				$GLOBALS['log']->debug("Processing photo for field $field_name");
				
				if(empty($_FILES[$field_name]['name'])) {
					if(!empty($_REQUEST['clear_'.$field_name])) {
						// create a non UTF-8 name encoding
						// 176 + 36 char guid = windows' maximum filename length
						$old_file_name = $_REQUEST['old_'.$field_name];
						$end = (strlen($old_file_name) > 176) ? 176 : strlen($old_file_name);
						$stored_file_name = substr($old_file_name, 0, $end);

						$old_photo = $sugar_config['cache_dir'].'images/'.$bean->id.'_'.$old_file_name;
						$GLOBALS['log']->debug("Photo Field->uploadPhoto: User choose to remove old photo: ".$old_photo);
						unlink($old_photo);
						
						
						$bean->$field_name = '';
					}
				
				
					$GLOBALS['log']->debug("Photo Field: No photo was selected to be uploaded");
					continue;
				}
				global $sugar_config;

				//if a previous photo has been uploaded then remove it now
				if(!empty($_REQUEST['old_'.$field_name])) {
					// create a non UTF-8 name encoding
					// 176 + 36 char guid = windows' maximum filename length
					$old_file_name = $_REQUEST['old_'.$field_name];
					$end = (strlen($old_file_name) > 176) ? 176 : strlen($old_file_name);
					$stored_file_name = substr($old_file_name, 0, $end);

					$old_photo = $sugar_config['cache_dir'].'images/'.$bean->id.'_'.$old_file_name;
					$GLOBALS['log']->debug("Photo Field->uploadPhoto: Deleting old photo: ".$old_photo);
					unlink($old_photo);
				}

				$file_name = $bean->id.'_'.$_FILES[$field_name]['name'];
				//save the file name to the database
				//$bean->contact_photo_c = $_FILES[$field_name]['name'];
				$bean->$field_name = $_FILES[$field_name]['name'];

				if(!is_uploaded_file($_FILES[$field_name]['tmp_name'])) {
					die("ERROR: file did not upload");
					//return false;
				} elseif($_FILES[$this->field_name]['size'] > $sugar_config['upload_maxsize']) {
					die("ERROR: uploaded file was too big: max filesize: {$sugar_config['upload_maxsize']}");
				}

				// create a non UTF-8 name encoding
				// 176 + 36 char guid = windows' maximum filename length
				$end = (strlen($file_name) > 176) ? 176 : strlen($file_name);
				$stored_file_name = substr($file_name, 0, $end);

				$destination = $sugar_config['cache_dir'].'images/'.$stored_file_name;

				if(!is_writable($sugar_config['upload_dir'])) {
					die("ERROR: cannot write to directory: {$sugar_config['upload_dir']} for uploads");
				}

				//$destination = clean_path($this->get_upload_path($bean_id));
				if(!move_uploaded_file($_FILES[$field_name]['tmp_name'], $destination)) {
					$GLOBALS['log']->debug("ERROR: can't move_uploaded_file to $destination. You should try making the directory writable by the webserver");
					die("ERROR: can't move_uploaded_file to $destination. You should try making the directory writable by the webserver");
				}
			} //end for each file upload	
		} 

	}

}

?>